﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfClient
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            
            MQTTServer.GetInstance().SendMessage();
            MQTTServer.GetInstance().
           // var data = CreateSHA512Hash(this.txt1.Text, this.txt2.Text);
           // this.txt3.Text = data;
        }
        public static string CreateSHA512Hash(string Password, string Salt)
        {

           return SHA256EncryptUtil.CreateToken(Password, Salt);
        }


        public static class SHA256EncryptUtil
        {
            public static string CreateToken(string message, string secret)
            {
                secret = secret ?? "";
                var encoding = new System.Text.ASCIIEncoding();
                byte[] keyByte = encoding.GetBytes(secret);
                byte[] messageBytes = encoding.GetBytes(message);
                using (var hmacsha256 = new HMACSHA256(keyByte))
                {
                    byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
                    return Convert.ToBase64String(hashmessage);
                }
            }
            /// SHA256加密，并十六进制编码
            /// </summary>
            /// <param name="data"></param>
            /// <returns></returns>
            public static string SHA256EncryptString(string data)
            {
                byte[] bytes = System.Text.Encoding.UTF8.GetBytes(data);
                byte[] hash = SHA256Managed.Create().ComputeHash(bytes);

                StringBuilder builder = new StringBuilder();
                for (int i = 0; i < hash.Length; i++)
                {
                    builder.Append(hash[i].ToString("x2"));
                }
                return builder.ToString();
            }


            /// <summary>
            /// HMACSHA256加密
            /// </summary>
            /// <param name="message"></param>
            /// <param name="secret"></param>
            /// <returns></returns>
            public static string HmacSHA256(string message, string secret)
            {
                secret = secret ?? "";
                var encoding = new System.Text.UTF8Encoding();
                byte[] keyByte = encoding.GetBytes(secret);
                byte[] messageBytes = encoding.GetBytes(message);
                using (var hmacsha256 = new HMACSHA256(keyByte))
                {
                    byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
                    StringBuilder builder = new StringBuilder();
                    for (int i = 0; i < hashmessage.Length; i++)
                    {
                        builder.Append(hashmessage[i].ToString("x2"));
                    }
                    return builder.ToString();
                }
            }
        }


    }
}
